---
title: "Shell中特殊变量"
date: 2019-6-20
categories:
- linux
- shell
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org59be4f8">$ 表示参数</a>
<ul>
<li><a href="#orgc4efa87">$$</a></li>
<li><a href="#orgdd14cdc">$!</a></li>
<li><a href="#org668c1f3">$?</a></li>
<li><a href="#org8465b9b">$-</a></li>
<li><a href="#orgdda7163">$*</a></li>
<li><a href="#org46bb9a8">$@</a></li>
<li><a href="#org32d9e0e">$#</a></li>
<li><a href="#org71bb192">$0</a></li>
<li><a href="#orgb133211">$1～$n</a></li>
</ul>
</li>
<li><a href="#orga84568b">! 表示历史</a>
<ul>
<li><a href="#orge4838de">!$ :: 上次命令的参数</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org59be4f8">
<h2 id="org59be4f8">$ 表示参数</h2>
<div class="outline-text-2" id="text-org59be4f8">
</div>
<div class="outline-3" id="outline-container-orgc4efa87">
<h3 id="orgc4efa87">$$</h3>
<div class="outline-text-3" id="text-orgc4efa87">
<p>
Shell本身的PID（ProcessID） 
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgdd14cdc">
<h3 id="orgdd14cdc">$!</h3>
<div class="outline-text-3" id="text-orgdd14cdc">
<p>
Shell最后运行的后台Process的PID 
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org668c1f3">
<h3 id="org668c1f3">$?</h3>
<div class="outline-text-3" id="text-org668c1f3">
<p>
最后运行的命令的结束代码（返回值） 
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org8465b9b">
<h3 id="org8465b9b">$-</h3>
<div class="outline-text-3" id="text-org8465b9b">
<p>
使用Set命令设定的Flag一览 
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgdda7163">
<h3 id="orgdda7163">$*</h3>
<div class="outline-text-3" id="text-orgdda7163">
<p>
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 
使用下面的方法获取参数列表子集
</p>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"${@:1:1}"</span>; <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">第一个参数</span>
<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"${@:2:1}"</span>; <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">第二个参数</span>
<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"${@:3}"</span>; <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">从第三个参数开始后面所有</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org46bb9a8">
<h3 id="org46bb9a8">$@</h3>
<div class="outline-text-3" id="text-org46bb9a8">
<p>
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。 
</p>
<p>
下面的脚本用于测试$* $@，运行脚本： . ./args_test.sh v1 v2 "v3" "$BASH asdf"
#+Name test_args.sh
</p>
<div class="org-src-container">
<pre class="src src-shell"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">argc 是一个打印参数个数和参数列表的脚本</span>
<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'----------$*----------'</span>
argc $<span style="font-weight: bold; font-style: italic;">*</span>
<span style="font-style: italic;">'''</span>
<span style="font-style: italic;">5</span>
<span style="font-style: italic;">Arg:  v1</span>
<span style="font-style: italic;">Arg:  v2</span>
<span style="font-style: italic;">Arg:  v3</span>
<span style="font-style: italic;">Arg:  /bin/bash</span>
<span style="font-style: italic;">Arg:  asdf</span>
<span style="font-style: italic;">'''</span>

<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'----------"$*"----------'</span>
argc <span style="font-style: italic;">"$*"</span>
<span style="font-style: italic;">'''</span>
<span style="font-style: italic;">1</span>
<span style="font-style: italic;">Arg:  v1 v2 v3 /bin/bash asdf</span>
<span style="font-style: italic;">'''</span>

<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'----------$@----------'</span>
argc $<span style="font-weight: bold; font-style: italic;">@</span>
<span style="font-style: italic;">'''</span>
<span style="font-style: italic;">5</span>
<span style="font-style: italic;">Arg:  v1</span>
<span style="font-style: italic;">Arg:  v2</span>
<span style="font-style: italic;">Arg:  v3</span>
<span style="font-style: italic;">Arg:  /bin/bash</span>
<span style="font-style: italic;">Arg:  asdf</span>
<span style="font-style: italic;">'''</span>

<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'----------"$@"----------'</span>
argc <span style="font-style: italic;">"$@"</span>
<span style="font-style: italic;">'''</span>
<span style="font-style: italic;">4</span>
<span style="font-style: italic;">Arg:  v1</span>
<span style="font-style: italic;">Arg:  v2</span>
<span style="font-style: italic;">Arg:  v3</span>
<span style="font-style: italic;">Arg:  /bin/bash asdf</span>
<span style="font-style: italic;">'''</span>
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">这里看出与 $* 之间的区别。所以 "$@" 适用于将所有参数传递给另外一个程序的场景</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org32d9e0e">
<h3 id="org32d9e0e">$#</h3>
<div class="outline-text-3" id="text-org32d9e0e">
<p>
添加到Shell的参数个数 
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org71bb192">
<h3 id="org71bb192">$0</h3>
<div class="outline-text-3" id="text-org71bb192">
<p>
Shell本身的文件名 
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgb133211">
<h3 id="orgb133211">$1～$n</h3>
<div class="outline-text-3" id="text-orgb133211">
<p>
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。 
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orga84568b">
<h2 id="orga84568b">! 表示历史</h2>
<div class="outline-text-2" id="text-orga84568b">
</div>
<div class="outline-3" id="outline-container-orge4838de">
<h3 id="orge4838de">!$ :: 上次命令的参数</h3>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-6-20</p>
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
